﻿<%@ Page Language="C#" MasterPageFile="~/Manager.master" AutoEventWireup="true" CodeFile="AddDownload.aspx.cs"
    Inherits="Manager_Download_AddDownload" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div align="left">
        <table style="width: 100%" border="1" cellpadding="2" cellspacing="2" class="tbl_Comon-Search" id="tblBodyMain">
            <tr>
                <td align="left" class="td_Control" colspan="2">
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="grpAdd" />
                </td>
            </tr>
            <tr>
                <td class="td_Title" style="width: 131px">
                    Tiêu đề</td>
                <td align="left" class="td_Control">
                    <asp:TextBox ID="txtTheLoai" runat="server" Width="92%" ValidationGroup="grpAdd"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="td_Title" style="width: 131px" valign="middle">
                    <asp:Label ID="Label1" runat="server" Text="Chọn file"></asp:Label></td>
                <td align="left" class="td_Control">
                    <table border="0" cellpadding="2" cellspacing="0" width="100%">
                        <tr>
                            <td align="left">
                                <div id="divFile" runat="server">
                                    <asp:DataList ID="dtlDetailFile" runat="server" OnItemDataBound="dtlDetailFile_ItemDataBound1">
                                        <ItemTemplate>
                                            <table border="0" cellpadding="2" cellspacing="0">
                                                <tr>
                                                    <td>
                                                        <asp:HyperLink ID="hplLink" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>'
                                                            NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"Link") %>'></asp:HyperLink>
                                                    </td>
                                                    <td>
                                                        <a href="#">
                                                            <img id="imgDel" onclick='fnDelFile(this)' link='<%# DataBinder.Eval(Container.DataItem,"Link") %>'
                                                                iddd='<%# DataBinder.Eval(Container.DataItem,"IdDd") %>' src="../../Images/Icon/cmdDelete.gif"
                                                                style="cursor: cursor;" border="0" /></a>
                                                    </td>
                                                </tr>
                                            </table>
                                        </ItemTemplate>
                                    </asp:DataList>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input id="btnAdd" class="cssButton" type="button" value="Thêm file" onclick="AddNewFile()" />
                                <asp:DataList ID="dtlFile" runat="server" CellPadding="0" CellSpacing="0" Width="100%">
                                    <ItemTemplate>
                                        <asp:Label ID="lblIndex" Text="<%#Container.ItemIndex%>" runat="server" Style="visibility: hidden"></asp:Label>
                                        <table border="0" cellpadding="2" cellspacing="0" width="100%">
                                            <tr>
                                                <td>
                                                    <asp:TextBox ID="txtName" runat="server" Style="background-color: WhiteSmoke; border: solid 1px DarkGray;
                                                        height: 16px" Text="tên file..." Width="160px" onblur="javascript:if(this.value==''){this.value='tên file...';};"
                                                        onfocus="javascript:if(this.value=='tên file...'){this.value='';};"></asp:TextBox>
                                                </td>
                                                <td nowrap="nowrap">
                                                    <asp:FileUpload ID="FileUpd" runat="server" />
                                                    <font color="blue"><b>or</b> </font>
                                                </td>
                                                <td width="70%">
                                                    <asp:TextBox ID="txtLink" runat="server" Text="http://" Style="background-color: Seashell;
                                                        border: solid 1px DarkGray; height: 16px" Width="95%" onblur="javascript:if(this.value==''){this.value='http://';};"
                                                        onfocus="javascript:if(this.value=='http://'){this.value='';};"></asp:TextBox>
                                                </td>
                                                <td width="10%">
                                                    <a href="#">
                                                        <img id="imgDel" runat="server" onclick="fnDelTableRow(this)" src="../../Images/Icon/cmdDelete.gif"
                                                            style="cursor: cursor;" border="0" /></a>
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                </asp:DataList></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td class="cssManage_left" colspan="2">
                </td>
            </tr>
            <tr>
                <td style="width: 131px">
                </td>
                <td style="height: 24px" align="left">
                    <input id="btnSave" class="cssButton" type="button" value="Bảo lưu" onclick="fnSaveFile()" />
                    <asp:Button ID="btnCancel" runat="server" CssClass="cssButton" Text="Trở về" OnClick="btnCancel_Click"
                        Width="80px" CausesValidation="False" /></td>
            </tr>
        </table>
    </div>
    <input type="hidden" id="hImg" name="hImg" runat="server" />
    <input type="hidden" id="hFileName" name="hFileName" runat="server" />
    <input type="hidden" id="hFile" name="hFile" runat="server" />
    <input id="hLink" runat="server" name="hLink" type="hidden" />
    <input id="hMethod" runat="server" name="hMethod" type="hidden" />

    <script language="javascript" type="text/javascript">
    String.prototype.trim = function () {
		return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}
	
     function fnDelFile(obj)
     {
        if(confirm('Delete???'))
        {
            var IdDd= obj.getAttribute("IdDd");   
            var Link=obj.getAttribute("Link");
            var IdD='<%=Request.QueryString["IdD"]%>';
            Manager_Download_AddDownload.AjaxDelFile(IdDd,Link,IdD,AjaxReturn)
        }
     }
     function AjaxReturn(res)
     {
        if(res!=undefined)
        {
            var divFile=document.getElementById("<%= divFile.ClientID%>") ;
            var dtl=res.value;
            divFile.innerHTML=dtl;
        }
     }
     function AddNewFile()
     {
        var tblMaster=document.getElementById("<%=dtlFile.ClientID%>");       
       
		    //-------
            var tr=tblMaster.insertRow(tblMaster.rows.length);		
				//
				var td=document.createElement('td');
				tr.appendChild(td);						
				var tableFIle=tblMaster.rows[0].cells[0];  				      
				td.innerHTML= tableFIle.innerHTML;			
				tblMaster.rows[tblMaster.rows.length-1].cells[0].children[0].innerHTML=tblMaster.rows.length-1;			
				
				 var tbl=document.getElementById("tblBodyMain"); 
                 parent.fnFullFrame(tbl.clientHeight);
				
     }
     function fnDelTableRow(objImg)
	 {
	    var index=objImg.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.children[0].innerHTML;
	    var tblMaster=document.getElementById("<%=dtlFile.ClientID%>");
	    if(tblMaster.rows.length<=1)
	    {
	        alert('Không thể xóa thêm'); return;
	    }
	    if(confirm('Delete???')) tblMaster.deleteRow(index);	    
	    for(var i=0;i<tblMaster.rows.length;i++)
	    {
	        tblMaster.rows[i].cells[0].children[0].innerHTML=i;
	    }
	 }
	 function fnSaveFile()
	 {
	    var txtTheLoai=document.getElementById("<%=txtTheLoai.ClientID%>");
	    if(txtTheLoai.value.trim()=="")
	    {
	        alert('Nhập vào tiêu đề');return;
	    }
	    
	    var tblMaster=document.getElementById("<%=dtlFile.ClientID%>");
	    var hFile=document.getElementById("<%=hFile.ClientID%>");
	    var hFileName=document.getElementById("<%=hFileName.ClientID%>");	    
	    var hLink=document.getElementById("<%=hLink.ClientID%>");	    	    
	    hFile.value="";
	    hFileName.value="";
	    hLink.value="";
	      
        for(var i=0;i<tblMaster.rows.length;i++)
        {
           var txtName=tblMaster.rows[i].cells[0].children[1].rows[0].cells[0].children[0];	 
           var objFile=tblMaster.rows[i].cells[0].children[1].rows[0].cells[1].children[0]; 
           var txtLink=tblMaster.rows[i].cells[0].children[1].rows[0].cells[2].children[0];     
            if(txtName.value.trim()=="" || txtName.value.trim()=="tên file...")   
            {
                alert('Nhập vào tên file') ;
                txtName.focus();
                return; 
            }  
            	       
            var sFile=objFile.value; 
            if(sFile.trim()=="" && txtLink.value=="http://" || txtLink.value=="") 
           {
                alert('Chọn Link hoặc file để upload') ;
       	        txtLink.focus();
                return;  
           }  

            var iLength=parseInt(sFile.length);
            var Index=parseInt(sFile.lastIndexOf("\\"));
            var file=sFile.substr(Index+1,iLength);   
            if(i<tblMaster.rows.length-1)
	        {
		        hFile.value+=file+"＊"; 
		        hFileName.value+=txtName.value+"＊";    
		        hLink.value+=txtLink.value+"＊";
	        }
	        else
	        {
		        hFile.value+=file; 
		        hFileName.value+=txtName.value;    
		        hLink.value+=txtLink.value;
	        }	
        }
       
	    var theForm=document.forms["aspnetForm"];

		var hMethod=document.getElementById("<%= hMethod.ClientID%>");
		hMethod.value="Save_Click";
		theForm.submit();

	 }
	
    </script>

</asp:Content>
